namespace comp {
    export class JoyStick extends eui.Group implements GameController {
        private btn: egret.DisplayObject   //按钮
        private bg: egret.DisplayObject //背景
        private centerX: number = 0
        private centerY: number = 0
        private bgRadius: number = 0
        private btnRadius: number = 0
        private touchID: number          //触摸ID
        private myParent: eui.Group

        public Angle: number    //0~2Pi
        public Distance: number //0~1

        public constructor() {
            super()
            this.name = "JoyStick"
        }
        public Type(): GameControllerKind { return GameControllerKind.JoyStick }
        public noSkin() {
            this.bgRadius = 120
            this.bg = ui.drawCircle({
                lineColor: Color.GRAY,
                lineAlpha: 0.8,
                fillColor: Color.GRAY,
                fillAlpha: 0.2,
                radius: this.bgRadius,
            })
            this.addChild(this.bg)

            this.btnRadius = 30
            this.btn = ui.drawCircle({
                lineColor: Color.GRAY,
                lineAlpha: 0.8,
                fillColor: Color.GRAY,
                fillAlpha: 0.6,
                radius: this.btnRadius,
            })
            this.addChild(this.btn)
            this.initData()
        }
        public childrenCreated() {
            console.debug("Joystick.childrenCreated")
            this.initData()
        }

        private initData() {
            this.centerX = this.bg.height / 2
            this.centerY = this.bg.height / 2
            this.bgRadius = this.bg.height / 2
            this.btnRadius = this.btn.height / 2
            this.anchorOffsetX = this.centerX
            this.anchorOffsetY = this.centerY
            this.bg.x = this.centerX
            this.bg.y = this.centerY
            this.btn.x = this.centerX
            this.btn.y = this.centerY

            this.bg.touchEnabled = false
            this.btn.touchEnabled = false
        }

        public start(parent: eui.Group) {
            this.myParent = parent
            this.myParent.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onTouchBegin, this);
            this.myParent.addEventListener(egret.TouchEvent.TOUCH_END, this.onTouchEnd, this);
            this.myParent.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.onTouchMove, this);
        }
        public stop() {
            this.myParent.removeEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onTouchBegin, this);
            this.myParent.removeEventListener(egret.TouchEvent.TOUCH_END, this.onTouchEnd, this);
            this.myParent.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.onTouchMove, this);
            this.myParent = null
        }

        private onTouchBegin(e: egret.TouchEvent) {
            if (this.parent) {
                return;
            }
            if (!this.myParent) {
                return
            }
            let x = e.stageX / game.scale - game.paddingLR
            let y = e.stageY / game.scale - game.paddingTB
            //let x = e.localX
            //let y = e.localY
            // let targe = e.target as egret.DisplayObject
            // while (target != this.myParent) {
            //     target = target.parent
            // }
            this.touchID = e.touchPointID;
            this.x = x;
            this.y = y;
            this.btn.x = this.centerX;
            this.btn.y = this.centerY;
            this.myParent.addChild(this)
            game.notify(ntf.JoyStickBegin, { Target: this })
        }
        private onTouchEnd(e: egret.TouchEvent) {
            if (this.touchID != e.touchPointID) {
                return;
            }
            this.hide();
            game.notify(ntf.JoyStickEnd, { Target: this })
        }
        private hide() {
            this.parent && this.parent.removeChild(this);
        }

        private p1: egret.Point = new egret.Point()
        private p2: egret.Point = new egret.Point()
        private onTouchMove(e: egret.TouchEvent) {
            if (this.touchID != e.touchPointID) {
                return;
            }

            let x = e.localX
            let y = e.localY
            let target = e.target as egret.DisplayObject
            while (target != e.currentTarget) {
                x += target.x - target.anchorOffsetX
                y += target.y - target.anchorOffsetY
                target = target.parent
            }

            this.p1.x = this.x
            this.p1.y = this.y
            this.p2.x = x
            this.p2.y = y
            var dist = egret.Point.distance(this.p1, this.p2);
            var angle: number = Math.atan2(y - this.y, x - this.x);
            if (dist <= (this.bgRadius)) {
                this.btn.x = this.centerX + x - this.x
                this.btn.y = this.centerY + y - this.y
                this.Distance = dist / this.bgRadius
            } else {
                this.btn.x = Math.cos(angle) * (this.bgRadius) + this.centerX
                this.btn.y = Math.sin(angle) * (this.bgRadius) + this.centerY
                this.Distance = this.bgRadius / this.bgRadius
            }

            this.Angle = angle
            while (this.Angle < 0) {
                this.Angle += 2 * Math.PI
            }

            game.notify(ntf.JoyStickChange, { Target: this, Distance: this.Distance, Angle: this.Angle }, this)
        }

    }
}